home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!ix-ham-oh1-12
- From: i82much@ix.netcom.com
- Newsgroups: comp.lang.c++
- Subject: Re: 2 D arrays in C++ constructors
- Date: 31 Mar 1996 11:27:08 GMT
- Organization: Netcom
- Message-ID: <4jlq6c$r4q@reader2.ix.netcom.com>
- References: <4jlcn2$kvl@csugrad.cs.vt.edu>
- NNTP-Posting-Host: ix-ham-oh1-12.ix.netcom.com
- X-NETCOM-Date: Sun Mar 31 3:27:08 AM PST 1996
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- In article <4jlcn2$kvl@csugrad.cs.vt.edu>,
- rsuri@csugrad.cs.vt.edu (Raj Suri) wrote:
- >Ok,
- >I wrote a c++ class that used an array of integers. However, I didn't know
- the
- >size of the array until the constructor was called. So I did this:
- >
- >class ... {
- >
- > private:
- > int* ptr;
- >
- >And then in the constructor, I had this code:
- >
- > ptr = new int[10];
- >
- >This works fine. However, now I need to use a two dimensional array in
- >which I don't know the dimensions until the constructor is called. I can't
- >figure out how to declare ptr and allocate space to it such that I could do
- the
- >following statement:
- >
- >ptr[2][3] = 4;
- >
- >Any help.. Thanks in advance.
- >-Raj
- >
-
- I think this may work (No guarantees)
-
- ptr = new int[a * b]
-
- where a is your fist indece and b is your second
-
- if ptr is a 4 by 5 array then ptr= new int [4 * 5]
-
- *** I'm not sure if ptr = new int[a][b] will work though ***
-
- Hope it helps my friend
-
- i82much@ix.netcom.com
-